package com.hlj.ncp.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlj.ncp.business.entity.Supplier;
import com.hlj.ncp.business.service.ISupplierService;
import com.hlj.ncp.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 供应商管理 控制器
 */
@Controller
@RequestMapping("/business/supplier")
public class SupplierController {
    
    @Autowired
    private ISupplierService supplierService;
    
    /**
     * 获取供应商列表
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(Supplier supplier, 
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        // 验证分页参数
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        Page<Supplier> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        
        // 设置查询条件
        if (supplier.getSupplierName() != null && !supplier.getSupplierName().isEmpty()) {
            queryWrapper.like(Supplier::getSupplierName, supplier.getSupplierName());
        }
        if (supplier.getSupplierType() != null && !supplier.getSupplierType().isEmpty()) {
            queryWrapper.eq(Supplier::getSupplierType, supplier.getSupplierType());
        }
        if (supplier.getStatus() != null && !supplier.getStatus().isEmpty()) {
            queryWrapper.eq(Supplier::getStatus, supplier.getStatus());
        }
        
        queryWrapper.orderByDesc(Supplier::getCreateTime);
        
        Page<Supplier> resultPage = supplierService.page(page, queryWrapper);
        return AjaxResult.success(resultPage);
    }
    
    /**
     * 获取供应商详细信息
     */
    @ResponseBody
    @GetMapping("/{supplierId}")
    public AjaxResult getInfo(@PathVariable Long supplierId) {
        return AjaxResult.success(supplierService.getById(supplierId));
    }
    
    /**
     * 新增供应商
     */
    @ResponseBody
    @PostMapping
    public AjaxResult add(@RequestBody Supplier supplier) {
        return supplierService.save(supplier) ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 修改供应商
     */
    @ResponseBody
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier) {
        return supplierService.updateById(supplier) ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 删除供应商
     */
    @ResponseBody
    @DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds) {
        for (Long supplierId : supplierIds) {
            supplierService.removeById(supplierId);
        }
        return AjaxResult.success();
    }

    /**
     * 返回供应商管理页面
     */
    @GetMapping("")
    public String index() {
        return "business/supplier/index";
    }
} 